/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.surveilans.model;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Master
 */
public class DetectModel extends Model{
    
    private InfoModel info;
    
    public DetectModel() {
        super();
        this.info = new InfoModel();
    }
    
    public boolean checkRuleB(int kabupaten,int penyakit){
        Calendar cal = Calendar.getInstance();
        DateFormat dfm = new SimpleDateFormat("yyyy-MM-dd");
        int today = info.getInfo(penyakit, kabupaten, cal.get(Calendar.YEAR), cal.get(Calendar.MONTH)+1, cal.get(Calendar.DATE)).getTotal();
        System.out.println(today);
        cal.add(Calendar.DATE, -1);
        int yesterday = info.getInfo(penyakit, kabupaten, cal.get(Calendar.YEAR), cal.get(Calendar.MONTH)+1, cal.get(Calendar.DATE)).getTotal();
        System.out.println(yesterday);
        cal.add(Calendar.DATE, -1);
        int twodaysago = info.getInfo(penyakit, kabupaten, cal.get(Calendar.YEAR), cal.get(Calendar.MONTH)+1, cal.get(Calendar.DATE)).getTotal();
        System.out.println(twodaysago);
        if(today==0 || yesterday==0 || twodaysago==0){
            return false;
        }
        else if((today>yesterday) && (yesterday>twodaysago)){
            System.out.println("rule b activated");
            return true;
        }else{
            return false;
        }
    }
    
    
    
    public boolean checkRuleC(int kabupaten,int penyakit){
        Calendar cal = Calendar.getInstance();
        DateFormat dfm = new SimpleDateFormat("yyyy-MM-dd");
        int today = info.getInfo(penyakit, kabupaten, cal.get(Calendar.YEAR), cal.get(Calendar.MONTH)+1, cal.get(Calendar.DATE)).getTotal();
        cal.add(Calendar.DATE, -1);
        int yesterday = info.getInfo(penyakit, kabupaten, cal.get(Calendar.YEAR), cal.get(Calendar.MONTH)+1, cal.get(Calendar.DATE)).getTotal();
        if(today==0 || yesterday==0){
            return false;
        }
        else if(today >= (2*yesterday)){
            return true;
        }else{
            return false;
        }
    }

    public void checkKLB(int kabupaten,int penyakit) {
        if(checkRuleB(kabupaten, penyakit) || checkRuleC(kabupaten, penyakit)){
            String kab = info.getNamaKabupaten(kabupaten);
            String pen = info.getNamaPenyakit(penyakit);
            this.sendSMSKLB("0008111803190", "Terjadi ancaman KLB untuk penyakit "+pen+" di kabupaten "+kab);
        }
    }
    
    public void sendSMSKLB(String to, String text) {
        PreparedStatement stmt = null;
        try {
            String query = "INSERT INTO outbox "
                    + "(DestinationNumber,Coding,TextDecoded,CreatorID) "
                    + "VALUES ('"+to+"','Default_No_Compression','"+text+"','admin')";
            stmt = con.prepareStatement(query, Statement.RETURN_GENERATED_KEYS);
            stmt.getGeneratedKeys();
            stmt.executeUpdate();
        } catch (SQLException ex) {
            Logger.getLogger(DetectModel.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
